package com.bjpower.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpower.convent.PayWayVoConvent;
import com.bjpower.domain.PayWay;
import com.bjpower.models.Result;
import com.bjpower.models.request.PayWayConfigRequest;
import com.bjpower.models.request.RemovePayWayRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.models.vo.PayWayDicVo;
import com.bjpower.models.vo.PayWayVo;
import com.bjpower.services.PayWayService;
import com.bjpower.services.impl.PayWayListServiceImpl;
import com.bjpower.settings.BossConfiguration;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class PayWayListController {
    @Resource
    private PayWayService payWayService;

    @Resource
    private PayWayVoConvent payWayVoConvent;

    @GetMapping("/payWay/list")
    public Result pagePayWayList(@Validated UserPageInfoRequest payWayRequest){
        IPage<PayWay> payWayList = payWayService.getPayWayList(payWayRequest);
        if (payWayList != null){
            List<PayWay> records = payWayList.getRecords();
            long pages = payWayList.getPages();
            long current = payWayList.getCurrent();
            List<PayWayVo> list = payWayVoConvent.toPayWayVoList(records);

            HashMap<String, Object> data = new HashMap<>();
            data.put("list", list);
            data.put("pages", pages);
            data.put("current", current);
            return Result.ok(data);
        }
        return null;
    }

    @DeleteMapping("/payWay/delete")
    public Result deletePayWay(@RequestBody RemovePayWayRequest removePayWay){
        payWayService.deletePayWay(removePayWay);
        return Result.ok();
    }
    /**
     * 获取支付方式下拉列表
     */
    @GetMapping("/product/dic")
    public Result getPayWayDic(UserPageInfoRequest pageInfoRequest){
        //从犯redis中获取信息
        List<PayWayDicVo> dicList = payWayService.getProductDic(pageInfoRequest);
        return Result.ok(dicList);
    }
    /**
     * 获取支付方式下拉列表，支付方式是固定的存放在缓存当中在类加载时就是会执行
     */
    @GetMapping("/product/payWay")
    public Result getPayWay(){
        //从集合中取
        return Result.ok(BossConfiguration.dicPayWayVoList);
    }
    /**
     * 根据支付方式获取支付类型
     */
    @GetMapping("/paytype/dic")
    public Result getPayType(String code){
        if (code != null){
            List<PayWayDicVo> dicPayTypeList = BossConfiguration.dicPayWayMap.get(code);
            return Result.ok(dicPayTypeList);
        }
        return Result.ok(new ArrayList<>());
    }

    /**
     * 创建支付方式
     */
    @PostMapping("/payWay/create")
    public Result createPayWay(@RequestBody @Validated PayWayConfigRequest payWayConfigRequest){
        payWayService.createPayWay(payWayConfigRequest);
        return Result.ok();
    }
}
